//
//  DDAppLauchViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/5.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDAppLauchViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIButton+DDUIButtonCategory.h"
#import <Masonry.h>
#import "DDPublicMacro.h"
#import "UILabel+DDUILabelCategory.h"
#import "DDLoginView.h"
#import "DDLogInTransitions.h"
#import <AVOSCloud.h>
#import "DDRegisterViewController.h"
#import "DDMainTabBarController.h"
//按钮的宽
#define BUTTON_W   (SCREEN_SIZE.width - 70)/2
//按钮的高
#define BUTTON_H    60
@interface DDAppLauchViewController ()<UIViewControllerTransitioningDelegate>
@property (nonatomic,strong)MPMoviePlayerController *moviePlayer;//视频播放器
@property (nonatomic,strong)DDLoginView* loginView;//登录页面
@end

@implementation DDAppLauchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
    [self registNotificationCenter];
}
- (void)viewWillDisappear:(BOOL)animated{
    [self.moviePlayer pause];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [self.moviePlayer play];
}
- (void)viewDidDisappear:(BOOL)animated{
self.loginView.hidden = YES;
}
#pragma mark UI设置
- (void)layoutUI{

///moviePlayer
    NSString *urlStr = [[NSBundle mainBundle]pathForResource:@"1.mp4" ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    self.moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    self.moviePlayer.view.frame = self.view.bounds;
    [self.view addSubview:self.moviePlayer.view];
     [self.moviePlayer.view.subviews[0] addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)]];
    self.moviePlayer.view.userInteractionEnabled = YES;
    self.moviePlayer.shouldAutoplay = YES;
    [self.moviePlayer setControlStyle:MPMovieControlStyleNone];
    [self.moviePlayer setFullscreen:YES];
    [self.moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:self.moviePlayer];
    [self.moviePlayer play];
///注册按钮
  
    UIButton* registerBtn = [UIButton dd_creatButtonTitle:@"注册" buttonType:UIButtonTypeRoundedRect font:17 titieColor:[UIColor whiteColor] bgColor:[UIColor blackColor] nomalImage:nil bgImage:nil];
    registerBtn.frame = CGRectMake(20,SCREEN_SIZE.height - BUTTON_H - 10 , BUTTON_W, BUTTON_H);
     registerBtn.alpha = 0.4;
    [registerBtn addTarget:self action:@selector(registerBtnClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:registerBtn];
///登录按钮
    UIButton* logInBtn = [UIButton dd_creatButtonTitle:@"登录" buttonType:UIButtonTypeRoundedRect font:17 titieColor:[UIColor blackColor] bgColor:[UIColor whiteColor] nomalImage:nil bgImage:nil];
    logInBtn.frame = CGRectMake(20+BUTTON_W+30,SCREEN_SIZE.height - BUTTON_H - 10 , BUTTON_W, BUTTON_H);
    logInBtn.alpha = 0.4;
    [logInBtn addTarget:self action:@selector(loginBtnClicked) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:logInBtn];
    
///中间应用文本
    UILabel* midLabel = [UILabel dd_CreatLabelWithTitle:@"Dai Dai Music" font:0 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentCenter];
    midLabel.font = [UIFont fontWithName:@"Bradley Hand" size:50];
    midLabel.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:midLabel];
    __weak  DDAppLauchViewController* weakSelf = self;
    [midLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.centerY.equalTo(weakSelf.view.mas_centerY).offset(-20);
        make.left.equalTo(@40);
        make.right.equalTo(@(-40));
        
        
    }];
//loginView
    
     self.loginView = [[DDLoginView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, logInBtn.frame.origin.y)];
    
    self.loginView.hidden = YES;
    [self.view addSubview:self.loginView];
 
}
#pragma make 逻辑操作部分
#pragma mark 登录按钮的回调方法
- (void)loginBtnClicked{

    self.loginView.hidden = NO;
    __weak DDAppLauchViewController* weakSelf = self;
    self.loginView.loginBtnClickedBlock = ^(){

        [weakSelf.loginView.passwordTextFiled resignFirstResponder];
        [weakSelf.loginView.accountTextField resignFirstResponder];
        if ([weakSelf.loginView.accountTextField.text isEqualToString:@""]) {
         [weakSelf.loginView.loginBtn failedAnimationWithPromptStr:@"请输入账号" Completion:nil];
            return;
        }
        if ([weakSelf.loginView.passwordTextFiled.text isEqualToString:@""]) {
             [weakSelf.loginView.loginBtn failedAnimationWithPromptStr:@"请输入密码" Completion:nil];
            return;
        }
    
//开始登录
        [AVUser logInWithUsernameInBackground:weakSelf.loginView.accountTextField.text password:weakSelf.loginView.passwordTextFiled.text block:^(AVUser *user, NSError *error) {

            if (error.code == -1009) {
                [weakSelf.loginView.loginBtn failedAnimationWithPromptStr:@"网络错误，请检查你的网络" Completion:nil];
                return;

            }
            if (user != nil) {
               [weakSelf.loginView.loginBtn succeedAnimationWithSucessType:loginSucessWithPresentType Completion:^{

                   [[NSUserDefaults standardUserDefaults]setObject:@"ISLOGIN" forKey:ISLOGIN];
             [weakSelf didPresentControllerButtonTouch:[DDMainTabBarController new]];
               }];
            } else {
                 [weakSelf.loginView.loginBtn failedAnimationWithPromptStr:@"账号或密码错误" Completion:nil];
                return;
            }
        }];
                     };

    

    

   
}
#pragma mark 注册按钮的回调方法
- (void)registerBtnClicked{
    DDRegisterViewController* registerViewController = [[DDRegisterViewController alloc]init];
    [self presentViewController:registerViewController animated:YES completion:nil];

}
#pragma mark 点击手势回调方法用于直接进入主页
- (void)tapView:(UITapGestureRecognizer*)tapGes{
   [UIApplication sharedApplication].keyWindow.rootViewController = [[DDMainTabBarController alloc]init];

}
#pragma mark 播放器状态的通知
-(void)playbackStateChanged{
    
    
    //取得目前状态
    MPMoviePlaybackState playbackState = [self.moviePlayer playbackState];
    
    //状态类型
    switch (playbackState) {
        case MPMoviePlaybackStateStopped:
            [self.moviePlayer play];
            break;
            
        case MPMoviePlaybackStatePlaying:
//            NSLog(@"播放中");
            break;
            
        case MPMoviePlaybackStatePaused:
            [self.moviePlayer play];
            break;
            
        case MPMoviePlaybackStateInterrupted:
//            NSLog(@"播放被中断");
            break;
            
        case MPMoviePlaybackStateSeekingForward:
//            NSLog(@"往前快转");
            break;
            
        case MPMoviePlaybackStateSeekingBackward:
//            NSLog(@"往后快转");
            break;
            
        default:
//            NSLog(@"无法辨识的状态");
            break;
    }
}
#pragma mark  登录界面框的回调方法
- (void)didPresentControllerButtonTouch:(UIViewController*)skipViewController{
    skipViewController.transitioningDelegate = self;
    [UIApplication sharedApplication].keyWindow.rootViewController = skipViewController;
    
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source {
    return [[DDLogInTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.5f isPush:true];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[DDLogInTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.8f isPush:false];
}


#pragma mark 隐藏系统的状态栏
- (BOOL)prefersStatusBarHidden{
    return YES;
    
}
#pragma mark 通知中心
#pragma mark 注册通知
- (void)registNotificationCenter{
//注册成功后的通知回调

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(registSucessedNotificationCenter:) name:@"DDRegisterViewControllerRegistSucceed" object:nil];

}
#pragma mark 通知的回调方法
- (void)registSucessedNotificationCenter:(NSNotification*)notification{
    [self loginBtnClicked];
    [self.loginView.loginBtn setTitle:@"登陆" forState:(UIControlStateNormal)];
    self.loginView.accountTextField.text = notification.object[@"userName"];
    self.loginView.passwordTextFiled.text = notification.object[@"passWord"];
    
    

}
@end
